package com.android.lovingpet.adopt.adoption.publish.view;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;


import android.database.Cursor;
import android.net.Uri;

import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.lovingpet.R;
import com.android.lovingpet.adopt.adoption.AdoptionPresenter;
import com.android.lovingpet.adopt.adoption.IAdoptionContract;
import com.android.lovingpet.adopt.adoption.view.AdoptionActivity;
import com.android.lovingpet.base.BaseFragment;
import com.android.lovingpet.bean.Pet;


import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static android.app.Activity.RESULT_OK;

/**
 * Created by ikata on 2018/4/26.
 */

public class AdoptionVideoFragment extends BaseFragment<IAdoptionContract.View, AdoptionPresenter> implements IAdoptionContract.View,View.OnClickListener {
    public final static int REQUEST_VIDEO_CODE=1;
    @BindView(R.id.et_demand)
    EditText etDemand;
    @BindView(R.id.iv_video)
    ImageView ivVideo;
    Unbinder unbinder;
    private Pet adoptPet;
    private File videoFile=null;
    @Override
    protected int setContentView() {
        return R.layout.fragment_adoption_video;
    }

    @Override
    protected void onCreateView(View view) {
        unbinder= ButterKnife.bind(this,view);
        EventBus.getDefault().register(this);
    }


    @Override
    public AdoptionPresenter createPresenter() {
        return new AdoptionPresenter();
    }

    @Override
    public IAdoptionContract.View createView() {
        return this;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
    }

    @OnClick({R.id.fab_publish,R.id.iv_video})
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.fab_publish:
                String demand=etDemand.getText().toString();
                if (!TextUtils.isEmpty(demand)) {
                    showProgress();
                    adoptPet.setDemand(demand);
                    getPresenter().addAdopt(adoptPet.getName(), String.valueOf(adoptPet.getType()), adoptPet.getBirthday(), adoptPet.getSterilize(), adoptPet.getSource(), adoptPet.getDescription(), adoptPet.getImmune(), adoptPet.getDemand(), adoptPet.getGender(),videoFile);
                }
                else
                {
                    Toast.makeText(getActivity(),"信息未补全!",Toast.LENGTH_LONG).show();
                }
                break;
            case R.id.iv_video:
                Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Video.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(intent, REQUEST_VIDEO_CODE);
                break;
            default:
                break;
        }
    }

    @Override
    public void next() {
        hideProgress();
        getActivity().finish();
    }

    @Override
    public void addPet(Pet pet) {

    }

    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void getPet(Pet pet)
    {
        adoptPet=pet;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==REQUEST_VIDEO_CODE&&resultCode==RESULT_OK&&data!=null){
           Uri uri=data.getData();
            ContentResolver cr=getContext().getContentResolver();
            if (uri!=null) {
                @SuppressLint("Recycle") Cursor cursor = cr.query(uri, null, null, null, null);
                if (cursor != null) {
                    if (cursor.moveToFirst()) {
                        // 视频路径：MediaStore.Audio.Media.DATA
                        String videoPath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
                        File file = new File(videoPath);
                        if (!file.exists()) {
                            Toast.makeText(getActivity(), "文件不存在!", Toast.LENGTH_LONG).show();
                            return;
                        }
                        if (file.length() > 1024 * 1024 * 50) {
                            Toast.makeText(getActivity(), "文件大于50mb!", Toast.LENGTH_LONG).show();
                            return;
                        }
                        videoFile = file;
                    }
                }
            }
        }
    }
    @SuppressLint("Recycle")
    public static String getPath(Context context, Uri uri) {

        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { "_data" };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                assert cursor != null;
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }

}
